/*
*
*/

#include <iostream>
#include "GhSqliteDbMgr.h"
#include <fstream>
#include <string.h>
#include <ftw.h>
#include "libbase64.h"

using namespace std;

#define MAX_COUNT 256
#define DATA_OFFSET 50000
#define WRITE_MODE 1
#define LUA_WRITE_PATH "/Users/guohao/Documents/Code/poker/src/app/"
#define DB_WRITE_PATH = "/Users/guohao/Documents/Code/poker/res/"

string replace( string &original, const string &subset, const string &section )
{
	if(subset == section or subset.empty())
		return original;

	for( string::size_type index = original.find( subset );
		 index not_eq string::npos;
		 index = original.find( subset, index ) )
	{
		original.replace( index, subset.size(), section );
		index += section.size();
	}

	return original;
}

int main(int argc,char** argv)
{
	int nMode = WRITE_MODE;

	if (argc == 2)
	{
		printf("Mode is %d\n",nMode);
		nMode = atoi(argv[1]);
	}

	FILE* fp = 0;
	GhSqliteDb* pkDb = 0;

	if (0 == nMode)
	{
		pkDb = g_pSqlite->createSqliteDb("temp.db");

		if (0 == pkDb)
		{
			cout << "Wrong!!" << endl;
			return -1;
		}

		if (!pkDb->isExistTable("name_list"))
		{
			pkDb->excuteSql("CREATE TABLE name_list(\
				id INTEGER PRIMARY KEY AUTOINCREMENT,\
				user_name char(64) NOT NULL UNIQUE);");
		}		
	}
	else if (1 == nMode)
	{
		char szFileName[1024] = {0};

		sprintf(szFileName,"%sGlobalNames.lua",LUA_WRITE_PATH);

		if (fp = fopen(szFileName,"w+"))
		{
			printf("打开文件成功\n");
			fseek(fp,0L,0);

			fputs("--[[\n	file: GlobalNames.lua\n	author:  Guo Hao\n	date:\
			    2015/07\n	note:\n	Auto gen file.\n]]\n",fp);

			fputs("local GlobalNames = GlobalNames or {};\nlocal p = GlobalNames;\n\n",fp);
			fputs("p.m_pAllNames =\n{\n",fp);
		}
		else
		{
			printf("打开文件成败\n");
			return -1;
		}		
	}

	fstream fin("acfun.txt");  //打开文件
	string strReadLine = "";

	std::vector<string> v;

	while(getline(fin,strReadLine))  //逐行读取，直到结束
	{
		string strCut = strReadLine;

		char* p = strtok((char*)strCut.c_str(),"	");

		if (0 == p)
		{
			cout << "Wrong word" << endl;
			continue;
		}

		string strAccount = p;

		p = strtok(NULL,"	");

		if (0 == p)
		{
			cout << "Wrong word" << endl;
			continue;
		}

		string strPasswd = p;

		p = strtok(NULL,"	");

		if (0 == p)
		{
			cout << "Wrong word" << endl;
			continue;
		}

		string strNick = p;

		//cout << strAccount << "   " << strPasswd << "   " << strNick << endl;

		if (6 > strNick.size())
		{
			continue;
		}

		v.push_back(strNick);
		strReadLine = "";
	}

	unsigned int uiSize = v.size();
	float fPercent = 0.0f;
	float fShowPercent = 0.0f;

	for (int i = DATA_OFFSET; i < uiSize; ++i)
	{
		if (i > MAX_COUNT + DATA_OFFSET)
		{
			printf("已经达到最大值了");
			break;
		}

		char szName[128] = {0};
		string strNick = v[i];
		Base64encode(szName,strNick.c_str(),strNick.length());
		strNick = szName;

		if (0 == nMode)
		{
			char szSql[128] = {0};

			sprintf(szSql,"replace into name_list (id,user_name) values (NULL,'%s');",strNick.c_str());

			if (!pkDb->excuteSql(szSql))
			{
				cout << "Error Data Is :" << strNick << endl;
				continue;
			}

			fPercent = (float)i / (float)uiSize;

			if (0.001f < fPercent - fShowPercent)
			{
				fShowPercent = fPercent;
				cout << "Current Complete Percent Is " << fShowPercent * 100.0f << "%" << endl;
			}
		}
		else if (1 == nMode)
		{
			char szCmd[1024] = {0};

			string strPoint = "";

			if ((uiSize > i + 1) && (MAX_COUNT + DATA_OFFSET > i))
			{
				strPoint = ",";
			}

			sprintf(szCmd,"	\"%s\"%s\n",strNick.c_str(),strPoint.c_str());

			fputs(szCmd,fp);
		}
	}

	fin.close();

 	cout << "size is " << v.size() << endl;

 	if (0 == nMode)
 	{
 		GhSqliteDBMgr::destroyInstance();
 	}
 	else if (1 == nMode)
 	{
 		fputs("};\n\n",fp);
 		fputs("return GlobalNames;",fp);
 		fclose(fp);
 	}

	return 0;
}